package com.zhuoyue.elmt.api;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.utils.StringUtils;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.modules.sys.entity.Dict;
import com.eit.eitsite.modules.sys.utils.UserUtils;
import com.google.common.collect.Lists;
import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.DeviceParameters;
import com.zhuoyue.elmt.entity.GgSbjc;
import com.zhuoyue.elmt.entity.MaintenanceRecordSz;
import com.zhuoyue.elmt.service.GgSbjcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping(value = "${adminPath}/api")
public class ApiController extends BaseController {

    @Autowired
    private GgSbjcService ggSbjcService;

    @RequestMapping(method = RequestMethod.POST, value = "/deviceParameters/updateSbLocation")
    public String updateSbLocation(DeviceParameters deviceParameters, HttpServletRequest request, HttpServletResponse response) {
        ResultData<List<GgSbjc>> resultData = new ResultData();
        try {
            // 同时更新设备的对应维保时间
            GgSbjc qryGgsbjc = new GgSbjc();
            qryGgsbjc.setSbzcdm(deviceParameters.getSbzcdm());
            GgSbjc ggSbjc = ggSbjcService.getBySbzcdm(qryGgsbjc);
            if (ggSbjc != null) {
                if (ggSbjc.getWbdwdm().equals(UserUtils.getUser().getCompany().getCode())) {
                    // 本单位设备
                    // ggSbjc.setSbzcdm(deviceParameters.getSbzcdm());
                    ggSbjc.setJd(deviceParameters.getJd());
                    ggSbjc.setWd(deviceParameters.getWd());
                    ggSbjcService.update(ggSbjc);

                    List<GgSbjc> list = Lists.newArrayList();
                    list.add(ggSbjc);

                    resultData.setList(list);

                    resultData.setList(list);
                    resultData.setCode("SUCC");
                    resultData.setMessage("");
                } else {
                    resultData.setCode("FAIL");
                    resultData.setMessage("该设备不属于你的单位，不能修改。");
                }
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("设备不存在，请确认系统中是否已添加设备。");
            }
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("采集坐标出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }
}
